// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_HR_CAR_RENTING
// * CreateDate    : 2013-08-28 11:00:56
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:56
// * LastChanger   : 2013-08-28 11:00:56
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Hr
{
/// <summary>
    /// HrCarRenting
    /// </summary>
    public partial class HrCarRentingBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objHrCarRentingEntity">Entity</param>
        /// <returns>Entity</returns>
        public static HrCarRentingEntity GetHrCarRenting(HrCarRentingEntity objHrCarRentingEntity)
        {
            return DataAccess.SelectSingle<HrCarRentingEntity>(objHrCarRentingEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objHrCarRentingEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertHrCarRenting(HrCarRentingEntity objHrCarRentingEntity)
        {
            return DataAccess.Insert(objHrCarRentingEntity);
        }

        public static bool InsertHrCarRenting(List<HrCarRentingEntity> objHrCarRentingEntityList)
        {
            return DataAccess.Insert(objHrCarRentingEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objHrCarRentingEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateHrCarRenting(HrCarRentingEntity objHrCarRentingEntity)
        {
            return DataAccess.Update(objHrCarRentingEntity);
        }

        public static bool UpdateHrCarRenting(List<HrCarRentingEntity> objHrCarRentingEntityList)
        {
            return DataAccess.Update<HrCarRentingEntity>(objHrCarRentingEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objHrCarRentingEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteHrCarRenting(HrCarRentingEntity objHrCarRentingEntity)
        {
            return DataAccess.Delete(objHrCarRentingEntity);
        }

        public static bool DeleteHrCarRenting(List<HrCarRentingEntity> objHrCarRentingEntityList)
        {
            return DataAccess.Delete<HrCarRentingEntity>(objHrCarRentingEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetHrCarRentingAll()
        {
            return DataAccess.SelectAllDataSet<HrCarRentingEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<HrCarRentingEntity> GetHrCarRentingAllList()
        {
            return DataAccess.SelectAll<HrCarRentingEntity>();
        }
        #endregion

        public static DataTable GetHrCarRentingByCondition(DTOHrCarRentingEntity condition)
        {
            string sql =  @"Select {0} CR.*, P.EMPLOYEE_NAME , 
								CASE WHEN CR.END_MILEAGE - CR.START_MILEAGE < 0 THEN 0 ELSE CR.END_MILEAGE - CR.START_MILEAGE END AS TOTAL_MILEAGE
                                FROM HR_CAR_RENTING CR
                                LEFT JOIN HR_EMPLOYEE P ON P.EMPLOYEE_NO = CR.PERSON_ID
                                WHERE 1=1 ";
            if (condition != null)
            {

                sql = string.Format(sql, condition.rowCount > 0 ? "Top " + condition.rowCount : "");
                if (condition.result != null)
                {
                    HrCarRentingEntity c = condition.result;

                    if (!string.IsNullOrEmpty(c.RentingId))
                        sql += " AND CR.RENTING_ID = '" + c.RentingId + "'";

                    if(!string.IsNullOrEmpty(c.DepartmentId))
                        sql += " AND CR.DEPARTMENT_ID = '" +c.DepartmentId + "'";

                    if (!string.IsNullOrEmpty(c.PersonId))
                        sql += " AND CR.PERSON_ID = '" + c.PersonId + "'";
                        
                     if (!string.IsNullOrEmpty(c.Status))
                        sql += " AND CR.STATUS = '" + c.Status+ "'";
                   
                    if (c.StartDate>DateTime.MinValue)
                    {
                        sql += " AND CR.START_DATE >= CONVERT(DATETIME,'" + (c.StartDate).ToString("yyyy-MM-dd") + " 00:00:00')";
                    }
                    if (c.EndDate > DateTime.MinValue)
                    {
                        sql += " AND CR.START_DATE <= CONVERT(DATETIME,'" + ((DateTime)c.EndDate).ToString("yyyy-MM-dd") + " 23:59:59')";
                    }
                }
            }

            sql += " ORDER BY START_DATE DESC" ;
          
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
	}
	
	
}	

